﻿using System;
using System.Linq;
using DotNetGenerics = System.Collections.Generic;

namespace Algoritmia.DataStructures.Sets
{
    public class HashSet<T> : ISet<T>
    {
        private readonly DotNetGenerics.HashSet<T> _hashSet;

        public HashSet(DotNetGenerics.IEnumerable<T> items=null)
        {
            items = items ?? Enumerable.Empty<T>();
            _hashSet = new DotNetGenerics.HashSet<T>(items);
        }

        #region ISet<T> Members

        public virtual bool Contains(T elem)
        {
            return _hashSet.Contains(elem);
        }

        public virtual void Add(T elem)
        {
            _hashSet.Add(elem);
        }

        public virtual void AddUnchecked(T elem)
        {
            _hashSet.Add(elem);
        }

        public virtual void Discard(T elem)
        {
            _hashSet.Remove(elem);
        }

        public virtual bool Remove(T elem)
        {
            return _hashSet.Remove(elem);
        }

        public virtual void Clear()
        {
            _hashSet.Clear();
        }

        #endregion

        #region ICountable Members

        public virtual int Count
        {
            get { return _hashSet.Count; }
        }

        #endregion

        #region IEnumerable<T> Members

        public virtual DotNetGenerics.IEnumerator<T> GetEnumerator()
        {
            return _hashSet.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _hashSet.GetEnumerator();
        }

        #endregion

        #region IEquatable<ISet<T>> Members

        public virtual bool Equals(ISet<T> other)
        {
            return Count == other.Count && this.IsSuperSetOf(other);
        }

        #endregion

        public override string ToString()
        {
            return String.Format("{0} <{1}> {{{2}}}", GetType().Name, typeof(T).Name, string.Join(",", this));
        }
    }
}
